//
//  GesturePasswordView.m
//  GesturePassword
//
//  Created by 好米有易 on 2017/9/7.
//  Copyright © 2017年 好米有易. All rights reserved.
//

#import "GesturePasswordView.h"
#import "UIButton+commots.h"
#import "UIColor+custom.h"
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width


@interface GesturePasswordView ()

/** 已选择按钮数组 */
@property(nonatomic, strong) NSMutableArray *selectedButtons;

/** 触摸位置 */
@property(nonatomic, assign) CGPoint currentTouchLocation;

@end

@implementation GesturePasswordView {
    
    CGFloat _circleWidth; // 圆的直径 75
    CGFloat _edgeMargin; // 距离边框的距离 20
    CGFloat _itemSpace; // item间距
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        _circleWidth = 75;
        _edgeMargin = 20;
        _itemSpace = (SCREEN_WIDTH - _edgeMargin * 2 - _circleWidth * 3) / 2;
        [self setupUI];
        
    }
    
    return self;
}

/** 初始化数组 */
- (NSMutableArray *)selectedButtons {
    if (_selectedButtons == nil) {
        
        _selectedButtons = [NSMutableArray array];
    }
    
    return _selectedButtons;
}

- (void)setupUI {
    
    for (int i = 0; i  < 9; i++) {
        
        // 计算frame
        
        // 同一行 i % 3
        NSInteger lineNum = i % 3;
        
        // 同一列 i / 3
        NSInteger columnNum = i / 3;
        
        // 计算 x
        CGFloat x = _edgeMargin  + (_circleWidth + _itemSpace) * lineNum;
        
        // 计算 y
        
        NSInteger y = _edgeMargin  + (_circleWidth + _itemSpace) * columnNum;
        
        CGRect frame = CGRectMake(x, y, _circleWidth, _circleWidth);
        
        UIButton *btn = [UIButton buttonWithImage:@"gesture_node_normal" selecteImage:@"gesture_node_highlighted"];
        
        btn.userInteractionEnabled = NO;
        
        btn.tag = i;
        
        btn.frame = frame;
        
        [self addSubview:btn];
        
        
    }
    
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [self touchesMoved:touches withEvent:event];
}


- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    UITouch *touch = [touches anyObject];
    
    CGPoint touchLocation = [touch locationInView:touch.view];
    
    for (UIButton *button in self.subviews) {
        
        if (CGRectContainsPoint(button.frame, touchLocation)) {
            
            button.highlighted  = YES;
            
            if (![self.selectedButtons containsObject:button]) {
                
                [self.selectedButtons addObject:button];
            }
            
        }
    
        self.currentTouchLocation = touchLocation;
        
    }
    
    [self setNeedsDisplay];
    
    
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    NSMutableString *passPath = [NSMutableString string];
    
    for (UIButton *button in self.selectedButtons) {
        
        button.highlighted = NO;
        
        [passPath appendFormat:@"%ld", button.tag];
    }
    
    NSLog(@"passPath -- %@", passPath);
    
    // 清空数组
    [self.selectedButtons removeAllObjects];
    
    // 重绘
    [self setNeedsDisplay];
    
}



- (void)drawRect:(CGRect)rect {
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    
    // 遍历已选择按钮数组
    for (int i = 0; i<self.selectedButtons.count; i++) {
        
        UIButton *button = self.selectedButtons[i];
        
        if (0 == i) {
            [path moveToPoint:button.center];
        } else {
            [path addLineToPoint:button.center];
        }
    }
    
    if (self.selectedButtons.count) {
        [path addLineToPoint:self.currentTouchLocation];
    }
    
    // 设置画笔
    [[UIColor colorWithR:212 g:246 b:254 a:121] set];
    [path setLineWidth:10];
    [path setLineCapStyle:kCGLineCapRound];
    [path setLineJoinStyle:kCGLineJoinBevel];
    
    [path stroke];
    
}





@end
